import OpenAI from "openai";
import fs from "fs"

//在运行环境中读取 github 的 token
const token = "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"
// const token = process.env["GITHUB_TOKEN"];
const endpoint = "https://models.inference.ai.azure.com";
const modelName = "gpt-4o"

function getImageDataUrl(imgUrl, imgFormat) {
  const imageBuffer = fs.readFileSync(imgUrl);
  const imageBase64 = imageBuffer.toString('base64')
  return `data:image/${imgFormat};base64,${imageBase64}`;
}
async function main() {
  const client = new OpenAI({ baseURL: endpoint, apiKey: token });

  const response = await client.chat.completions.create({
    model: modelName,
    temperature: 1.0,
    top_p: 1.0,
    messages: [
      { role: 'system', content: '帮我找一张图片，返回url地址' },
      {
        role: 'user', content: [
          { type: 'text', text: '帮我找一张图片，返回url地址' },
          // {
          //   type: 'image_url', image_url: {
          //     url: getImageDataUrl("book.jpg", "jpg"),
          //     detail: 'auto'
          //   }
          // }
        ]
      },
    ]
  })
  console.log(response.choices[0].message.content);

}

main()

